package com.jiuyi.filter;

import com.alibaba.fastjson.JSON;
import com.jiuyi.common.R;
import com.jiuyi.utils.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.AntPathMatcher;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * DATA: 2022/10/17
 * Author: 晨夕
 */
@WebFilter(filterName = "loginCheckFilter",urlPatterns = "/*")
@Slf4j
public class LoginCheckFilter implements Filter {

    //路径匹配器，支持通配符
    public static final AntPathMatcher PATH_MATCHER = new AntPathMatcher();

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        //1. 获取本次请求的URL
        String requestURI = request.getRequestURI();
        log.info("获取的请求： {}" ,requestURI);
        //2. 判断本次请求是否需要处理，不需要就放行
        //2.1 不需要处理的请求
        String[] strs = new String[]{
                "/employee/login",  //登录不要拦截
                "/employee/logout", //登出不要拦截
                "/backend/**",      //静态资源不要拦截
                "/front/**",
                "/common/**",
                "/user/sendMsg",
                "/user/login",
                "/doc.html",
                "/webjars/**",
                "/swagger-resources",
                "/v2/api-docs"
        };
        //2.2 判断
        boolean check = check(strs, requestURI);
        //3. 不需要处理的请求直接放行
        if(check){
            log.info("已放行：{}",requestURI);
            filterChain.doFilter(request, response);
            return;
        }
        //4.1 判断后台登录状态，已登录就放行
        if(request.getSession().getAttribute("employee") != null){
            log.info("已登录：{}",request.getSession().getAttribute("employee"));
            //获取id，将id放入ThreadLocal中
            Long id = (Long) request.getSession().getAttribute("employee");
            BaseContext.setCurrentId(id);
            filterChain.doFilter(request, response);
            return;
        }
        //4.2 判断前台登录状态，已登录就放行
        if(request.getSession().getAttribute("user") != null){
            log.info("已登录：{}",request.getSession().getAttribute("user"));
            //获取id，将id放入ThreadLocal中
            Long id = (Long) request.getSession().getAttribute("user");
            BaseContext.setCurrentId(id);
            filterChain.doFilter(request, response);
            return;
        }
        //5. 未登录返回结果
        response.getWriter().write(JSON.toJSONString(R.error("请登录")));
        return;
    }

    public boolean check(String[] strs , String requestURI){
        for(String str : strs){
            boolean match = PATH_MATCHER.match(str, requestURI);
            if(match){
                return true;
            }
        }
        return false;
    }
}
